﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Grundy.Common;

namespace Grundy.Message
{
    
    [DataContract]
    public class ClientSession
    {
        private static TimeSpan _timeout = new TimeSpan(0,8,0,0);
        
        public ClientSession()
        {
            LastSeenOn = Clock.Instance.Utc;
        }

        [DataMember]
        public DateTime LastSeenOn { get; protected set; }

        public Guid Id { get; set; }
        
        [DataMember]
        public IdentifierCollection Identifiers { get; set; }

        [IgnoreDataMember]
        public IdentifierCollection IdentifiersUsedToIdentify { get; set; }
        
        [DataMember]
        public FeatureIdentificationKey FeatureIdentificationKey { get; set; }

        public void Reassert()
        {
            LastSeenOn = Clock.Instance.Utc;
        }

        [IgnoreDataMember]
        public DateTime ExpiresOn { get { return LastSeenOn.Add(_timeout); } }
        
        [DataMember]
        public EndPoint EndPoint { get; set; }

        public bool HasExpired()
        {
            return Clock.Instance.Utc >= ExpiresOn;
        }
    }
}
